package lsl_orig;
import java.io.*;
import java.util.*;
//import us.inc.sccn.lsl.*;
//import us.inc.sccn.lsl.vectorinfo;
//import us.inc.sccn.lsl.stream_inlet;
//import us.inc.sccn.lsl.stream_info;
//import us.inc.sccn.lsl.vectorf;

public class lsl_iface {

	private  liblsl lsl;
	private  stream_info sinfo;
	private  stream_inlet sinlet;
	private  vectorinfo vector_info;
	private  vectorf sample;
	public   int counter = 0;
	private  Vector chunk = new Vector();
	private  String rootpath = "/Users/timmullen/Documents/WORK/Toolboxes/labstreaminglayer/LSL/liblsl-Java";
	private  final double TIMEOUT = 0; // timeout (in sec)

	//private static String rootpath = "/Users/timmullen/Documents/Processing/test_lsl/liblsl-java.jar";


	public void start_lsl(String selector, String value) {

		if (selector==null) {
			selector = "type";
		}
		if (value==null) {
			value = "EEG";
		}

		System.out.println("Loading library...");
		System.out.println(System.getProperty("user.dir"));

		// load the right wrapper library...
		if (System.getProperty("os.name").startsWith("Windows")) {
			if (System.getProperty("sun.arch.data.model").equals("32")) {
				System.load(System.getProperty("user.dir") + "/liblslJNI32.dll");
			} 
			else {
				System.load(System.getProperty("user.dir") + "/liblslJNI64.dll");
			}
		} 
		else {
			if (System.getProperty("os.name").startsWith("Mac")) {
				//System.out.println(System.getProperty("user.dir"));
				//System.load(System.getProperty("user.dir") + "/liblslJNI.jnilib");
				if (System.getProperty("sun.arch.data.model").equals("32")) {
					System.load(System.getProperty("user.dir") + "/liblslJNI32.jnilib");
				} 
				else {
					System.load(System.getProperty("user.dir") + "/liblslJNI64.jnilib");
				}
				//System.load(rootpath + "/liblslJNI.jnilib");
			} 
			else {
				System.load(System.getProperty("user.dir") + "/liblslJNI.so");
			}
		}

		lsl = new liblsl();
		vector_info = lsl.resolve_stream(selector, value);
		sample = new vectorf(15);
		sinlet = new stream_inlet(vector_info.get(0));

		System.out.println("Library loaded!");
	}

	// close inlet
	public void stop_lsl() {
		sinlet.close_stream();
	}


	/*
  public Vector get_chunk(int numSamples) {
   // delete old samples
   //vector.remove(numSamples);

   // initialize chunk data array
   for (int i = 0; i < numSamples; i++) {
   chunk.set(i, new Vector());
   }

   Double timestamp;
   chunk = new Vector<Float>();

   for (int t=0;t<numSamples;t++) { // for each time point...
   timestamp = sinlet.pull_sample(sample, TIMEOUT); // read samples from buffer
   //System.out.println("SIZE:" + sample.size());
   //System.out.println("----------------------------");

   if (timestamp==null || sample.size()==0) {
   // buffer is empty
   break;
   }

   for (int ch=0; ch<sample.size(); ch++) { // append all channel data
   ((Vector) chunk.get(t)).insertElementAt(sample.get(ch), ch);
   }
   }

   return chunk;
   }
	 */

	// Get a multivariate sample from the stream
	// Input:  pointer to a Vector to hold the multivariate sample
	// Output: size of sample vector (e.g. number of channels). 
	//         This is 0 if no samples were available
	public int get_sample(Vector sample_out) {

		double timestamp = sinlet.pull_sample(sample, TIMEOUT); // read samples from buffer

		if (timestamp==0.0 || sample.size()==0) // buffer is empty
			return 0;

		// initialize sample vector
		if (sample_out.isEmpty() || sample_out.size()!=sample.size())
			sample_out.setSize((int)sample.size());

		// JUST FOR DEBUGGING...
		if ((counter++ % 100) == 0)
			System.out.println(timestamp + ":\t");

		// copy elements into output vector
		for (int ch=0; ch<sample.size(); ch++) {
			sample_out.set(ch,sample.get(ch));
		}

		return (int) sample.size();

	}
}
